package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.DateUtil;
import com.ecoolex.hotel.saas.core.dao.PresaleCouponExpirydateDAO;
import com.ecoolex.hotel.saas.entity.PresaleCouponExpirydate;
import com.ecoolex.hotel.saas.entity.PresaleCouponExpirydate.Column;
import com.ecoolex.hotel.saas.enums.PresaleCouponExpirydateStrategy;
import com.ecoolex.hotel.saas.pojo.EditPresaleCouponExpirydateDTO;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PresaleCouponExpirydateManager {
	
    @Autowired
    private PresaleCouponExpirydateDAO dao;
    
    public void edit(Integer preCouponId, EditPresaleCouponExpirydateDTO expirydateDTO, String updateUser) {
    	if(expirydateDTO.getExpirydateStrategy().equals(PresaleCouponExpirydateStrategy.FIXED.real())) {
    		//固定有效期
    		BizAssert.isTrue(Check.notNull(expirydateDTO.getFixedStartExpirydate()),ResultCode.BIZ.build(19), "固定有效期开始时间不能为空");
    		BizAssert.isTrue(Check.notNull(expirydateDTO.getFixedEndExpirydate()),ResultCode.BIZ.build(20), "固定有效期结束时间不能为空");
    		DateUtil.cleanTimePart(expirydateDTO.getFixedStartExpirydate());
    		DateUtil.setMaxTimePart(expirydateDTO.getFixedEndExpirydate());
    		BizAssert.isTrue(Check.isLessThan(expirydateDTO.getFixedStartExpirydate(), expirydateDTO.getFixedEndExpirydate()),ResultCode.BIZ.build(21), "开始时间必须小于结束时间");
    		BizAssert.isTrue(Check.isGreatThan(expirydateDTO.getFixedEndExpirydate(), Date.from(Instant.now())),ResultCode.BIZ.build(22), "结束时间必须大于当前时间");
    		
    	}else if(expirydateDTO.getExpirydateStrategy().equals(PresaleCouponExpirydateStrategy.DYNAMIC.real())) {
    		//动态有效期
    		BizAssert.isTrue(Check.notNullAndGtZero(expirydateDTO.getDynamicExpirydateValue()),ResultCode.BIZ.build(23),"动态有效期时间不能为空");
    	}else {
    		BizAssert.throwBizException("有效期策略类型错误！");
    	}
    	PresaleCouponExpirydate expirydate = get(preCouponId);
    	if(Check.notNull(expirydate)) {
    		BeanUtil.copy(expirydateDTO, expirydate);
    		expirydate.setUpdateUser(updateUser);
    		expirydate.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKeySelective(expirydate, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
    	}else {
    		expirydate = new PresaleCouponExpirydate();
    		expirydate.setPreCouponId(preCouponId);
    		BeanUtil.copy(expirydateDTO, expirydate);
    		expirydate.setDeleted(PresaleCouponExpirydate.NOT_DELETED);
    		expirydate.setCreateUser(updateUser);
    		expirydate.setCreateTime(Date.from(Instant.now()));
    		expirydate.setUpdateUser(updateUser);
    		expirydate.setUpdateTime(Date.from(Instant.now()));
    		dao.insert(expirydate);
    	}
    }
    
    public void delete(Integer preCouponId, String updateUser) {
    	PresaleCouponExpirydate expirydate = get(preCouponId);
    	if(Check.notNull(expirydate)) {
    		expirydate.setDeleted(PresaleCouponExpirydate.IS_DELETED);
    		expirydate.setUpdateUser(updateUser);
    		expirydate.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKeySelective(expirydate, Column.deleted,Column.updateUser,Column.updateTime);
    	}
    }
    
    public PresaleCouponExpirydate get(Integer preCouponId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(preCouponId, PresaleCouponExpirydate.NOT_DELETED);
    }
    
}